package link.anyauto.j2m.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

import link.anyauto.j2m.MainAct;
import link.anyauto.j2m.R;

/**
 * Created by Administrator on 2016/5/5.
 */
public class ForegroundService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        showNotification();
    }

    void showNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.smile).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.master))
                .setContentTitle("晴 25℃").setContentText("今天有雨");
        Intent intent = new Intent(this, MainAct.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainAct.class).addNextIntent(intent);
        PendingIntent pi = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pi);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = builder.build();
        manager.notify(NOTIFY_ID, notification);
        startForeground(NOTIFY_ID, notification);
    }

    static final int NOTIFY_ID = 1;
    public static final String FOREGROUND_ACTION = "link.anyauto.j2m.FOREGROUND_SERVICE";
}
